﻿using System;
using System.Web.UI;

using CssAndJsCombiner.UI.WebControls;

namespace CssAndJsCombiner.UI
{
    public class BasePage : Page
    {
        public string StylesheetGroup { get; set; }
        public string JavascriptGroup { get; set; }

        public WebContext WebContext
        {
            get { return WebContext.Current; }
        }

        public bool IsDebugMode
        {
            get { return WebContext.IsDebugMode; }
        }

        protected override void OnPreRenderComplete(EventArgs e)
        {
            if (!String.IsNullOrEmpty(StylesheetGroup))
            {
                Stylesheet css = new Stylesheet() { Group = StylesheetGroup };
                RegisterStylesheet(css);
            }

            if (!String.IsNullOrEmpty(JavascriptGroup))
            {
                Javascript script = new Javascript() { Group = JavascriptGroup };
                RegisterJavascript(script);
            }

            base.OnPreRenderComplete(e);
        }

        public void RegisterJavascript(string path)
        {
            Javascript script = new Javascript() { Path = path };
            RegisterJavascript(script);
        }

        public void RegisterJavascript(Javascript script)
        {
            ScriptManager sm = ScriptManager.GetCurrent(this);
            if (sm != null)
            {
                string path = ClientResourceControl.AddVersion(script.Path, ClientResourceType.Js);
                sm.Scripts.Add(new ScriptReference(path));
            }
            else
            {
                registerControlInHead(script, script.Path);
            }
        }

        public void RegisterStylesheet(string path)
        {
            Stylesheet css = new Stylesheet() { Path = path };
            RegisterStylesheet(css);
        }

        public void RegisterStylesheet(Stylesheet css)
        {
            registerControlInHead(css, css.Path);
        }

        public Control FindControlRecursive(Control current, string controlID)
        {
            if (current == null) throw new ArgumentNullException("current");
            if (controlID == null) throw new ArgumentNullException("controlID");

            if (current.ID == controlID)
            {
                return current;
            }

            if (current.Controls.Count > 0)
            {
                foreach (Control c in current.Controls)
                {
                    Control t = FindControlRecursive(c, controlID);
                    if (t != null) return t;
                }
            }

            return null;
        }

        private void registerControlInHead(Control c, string key)
        {
            if (c == null) throw new ArgumentNullException("c");
            if (Header == null) throw new InvalidOperationException("Page need to have runat=\"server\" on its <head>");

            if (!Items.Contains(key))
            {
                Header.Controls.Add(c);
                Items[key] = true;
            }
        } 
    }
}
